package com.baymax.study.tuLing.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

/**
 * @author baymax
 * @since 2025-06-27
 */
public class NioNonBlockDemo3_Write {
    public static final int PORT = 60001;

    static class Server {
        public static void main(String[] args) throws Exception {
            Selector selector = Selector.open();

            ServerSocketChannel ssc = ServerSocketChannel.open();
            ssc.configureBlocking(false);
            ssc.bind(new InetSocketAddress(PORT));
            ssc.register(selector, SelectionKey.OP_ACCEPT);

            while (true) {
                selector.select();
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                while (iterator.hasNext()) {
                    SelectionKey key = iterator.next();
                    iterator.remove();
                    if (key.isAcceptable()) {
                        SocketChannel sc = ssc.accept();
                        sc.configureBlocking(false);
                        SelectionKey scKey = sc.register(selector, 0, null);

                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < 100000; i++) {
                            sb.append("a");
                        }
                        ByteBuffer buffer = StandardCharsets.UTF_8.encode(sb.toString());

                        int length = sc.write(buffer);
                        System.out.println("发送了" + length + "字节");

                        if (buffer.hasRemaining()) {
                            scKey.interestOps(key.interestOps() + SelectionKey.OP_WRITE);
                            scKey.attach(buffer);
                        }
                    }
                    if (key.isWritable()) {
                        SocketChannel sc = (SocketChannel) key.channel();
                        ByteBuffer buffer = (ByteBuffer) key.attachment();
                        int length = sc.write(buffer);
                        System.out.println("发送了" + length + "字节");
                        if (!buffer.hasRemaining()) {
                            key.attach(null);
                            key.interestOps(SelectionKey.OP_READ);
                        }
                    }
                }
            }
        }
    }

    static class Client {
        public static void main(String[] args) throws Exception {
            SocketChannel sc = SocketChannel.open();
            sc.connect(new InetSocketAddress("127.0.0.1", PORT));

            int count = 0;
            while (true) {
                ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);
                count += sc.read(buffer);
                System.out.println(count);
                buffer.clear();
            }
        }
    }
}
